#include "adcollector.h"
#include "modbus.h"
#include <QDebug>
#include <QTime>
ADCollector::ADCollector()
{
    this->moveToThread(&innerCycleCore);
    innerCycleCore.start();
    StartSignalInit();
    emit NewModbusDevice();
    modBusDevice->ConnectSignalSlots();
    SignalSlotsInit();
}

ADCollector::~ADCollector()
{
    qDebug()<<"~ADCollector start";
    timer->stop();
    innerCycleCore.quit();
    innerCycleCore.wait();
    qDebug()<<"~ADCollector stop";
}

void ADCollector::StartSignalInit()
{
    connect(this,SIGNAL(NewModbusDevice()),this,SLOT(Slot_NewModbusDevice()),Qt::BlockingQueuedConnection);
}


void ADCollector::SignalSlotsInit()
{
    connect(this,SIGNAL(PortOpen(QString)),this,SLOT(Slot_PortOpen(QString)),Qt::BlockingQueuedConnection);
    connect(timer,SIGNAL(timeout()),this,SLOT(Slot_TimerTimeout()));
    connect(this,SIGNAL(Signal_ToStartTimer()),this,SLOT(Start_Timer()),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(Signal_ToStopTimer()),this,SLOT(Stop_Timer()),Qt::BlockingQueuedConnection);

    connect(this,SIGNAL(Signal_ToSwitchToCurrentValue()),this,SLOT(Slot_ToSwitchToCurrentValue()),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(Signal_ToSwitchToVoltageValue()),this,SLOT(Slot_ToSwitchToVoltageValue()),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(Signal_ToGetWorkMode(ushort *)),this,SLOT(Slot_GetWorkMode(ushort*)),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(Signal_ToSendF3(uint8_t, ushort, ushort,ushort *,bool *)),this,SLOT(Slot_ToSendF3(uint8_t, ushort, ushort,ushort *,bool *)),Qt::BlockingQueuedConnection);
}

void ADCollector::SwitchToCurrentValue()
{
    emit Signal_ToSwitchToCurrentValue();
}

void ADCollector::SwitchToVotagetValue()
{
    emit Signal_ToSwitchToVoltageValue();
}

void ADCollector::OpenPort(QString value)
{
    emit PortOpen(value);
}

RecvAdValue ADCollector::GetCurrentValue()
{
    RecvAdValue recvAdValue;
    recvAdValue.startTime=QTime::currentTime();
    StopTimer();
    QThread::msleep(200);
    ushort recv[8]={0};
    float value[8]={0};
    bool ret;
    recvAdValue.isSuccess=false;
    emit Signal_ToSendF3(1,0,8,recv,&ret);
    //bool ret=modBusDevice->SendFc3(1,0,8,recv);
    if(ret)
    {
        for(int i=0;i<=7;i++)
        {
            if(testMode==0)
                value[i]=((float)recv[i]-0.00)/((float)65535-0)*(10.0000-(-10.0000))+-10.0000;
            else if(testMode==1)
                value[i]=((float)recv[i]-0.00)/((float)65535-0)*(40.00-0.00)+0.00;
            recvAdValue.readValue[i]=value[i];
        }
        recvAdValue.isSuccess=true;
        StartTimer();
        recvAdValue.endTime=QTime::currentTime();
        return recvAdValue;
    }
    else
    {
        StartTimer();
        recvAdValue.endTime=QTime::currentTime();
        return recvAdValue;
    }
}

void ADCollector::StartTimer()
{
    emit Signal_ToStartTimer();
}

void ADCollector::StopTimer()
{
    emit Signal_ToStopTimer();
}

int ADCollector::GetWorkMode()
{
    ushort buf[8]={0};
    int cnt1=0,cnt2=0;
   emit Signal_ToGetWorkMode(buf);
     //modBusDevice->SendFc3(1,200,8,buf);
    //QThread::msleep(1000);
    for(int i=0;i<=7;i++)
    {
        qDebug()<<buf[i];
        if(buf[i]==0x0008)
         {
            cnt1++;
            if(cnt1==8)
            {
                testMode=0;
                return testMode;
            }
        }
        else if(buf[i]==0x000f)
        {
            cnt2++;
            if(cnt2==8)
            {
                testMode=1;
                return testMode;
            }
        }
    }
    return 2;
}

void ADCollector::Slot_ToSwitchToCurrentValue()
{
    ushort value[8];
    for(int i=0;i<=7;i++)
        value[i]=0x000f;
    modBusDevice->SendFc16(1,200,8,value);
    qDebug()<<__FUNCTION__;
}

void ADCollector::Slot_ToSwitchToVoltageValue()
{
    ushort value[8];
    for(int i=0;i<=7;i++)
        value[i]=0x0008;
    modBusDevice->SendFc16(1,200,8,value);
    qDebug()<<__FUNCTION__;
}

void ADCollector::Slot_NewModbusDevice()
{
    modBusDevice=new Modbus();
    modBusDevice->port.setBaudRate(9600);
    modBusDevice->port.setParity(QSerialPort::NoParity);
    modBusDevice->port.setDataBits(QSerialPort::Data8);
    modBusDevice->port.setStopBits(QSerialPort::OneStop);
    timer=new QTimer();
}

void ADCollector::Slot_PortOpen(QString portName)
{
    modBusDevice->port.setPortName(portName);
    modBusDevice->port.open(QSerialPort::ReadWrite);
   // timer->start(1000);
}

void ADCollector::Slot_TimerTimeout()
{
    qDebug()<<QTime::currentTime()<<"~";
    ushort recv[8]={0};
    qDebug()<<__FUNCTION__<<__LINE__;
    modBusDevice->SendFc3(1,0,8,recv);
    qDebug()<<__FUNCTION__<<__LINE__;

    for(int i=0;i<=7;i++)
    {
        if(testMode==0)
            getValue[i]=((float)recv[i]-0.00)/((float)65535-0)*(10.0000-(-10.0000))+-10.0000;
        else if(testMode==1)
            getValue[i]=((float)recv[i]-0.00)/((float)65535-0)*(40.0000-(0.0000))+0;
        qDebug()<<recv[1]<<getValue[1];
    }
    emit Signal_ToShowValue(getValue);
}

void ADCollector::Stop_Timer()
{
    timer->stop();
}

void ADCollector::Start_Timer()
{
    timer->start(1000);
}

void ADCollector::Slot_GetWorkMode(ushort *buf)
{
    qDebug()<<__FUNCTION__<<__LINE__;
    modBusDevice->SendFc3(1,200,8,buf);
    qDebug()<<__FUNCTION__<<__LINE__;
}

void ADCollector::Slot_ToSendF3(uint8_t adress, ushort start, ushort number, ushort *recv, bool *ret)
{
    *ret=modBusDevice->SendFc3(adress,start,number,recv);
}
